from datetime import datetime,date
import d_sign
import time
import d_server
import ComFun

class Sign:
    ## signNum 周签到次数 sizeSignNum 28天中的总签到次数， dayOfWeek 星期几    weekNum 第几周 0都是第一次 curTime 上次时间

    def __init__(self):

        if not self.signWeek :
            self.signWeek = [0,0,0,0,0,0,0]
        if not self.signWeek:
            self.signMonth = [0, 0, 0, 0]
        # self.signNum = 0
        # self.score += 100
        # self.sizeSignNum = 6
        if not self.cntTime :
            self.cntTime = int(time.time())   #d_server.SetInitTime
        if not self.whatMonth :
            self.whatMonth = 0
        self.getWeeday()

    def getWeeday(self):
        # cnt = int(time.time()) - d_server.SetInitTime
        #self.whatMonth = int(cnt/d_server.TwentyeightDays) #第几个28天
        #self.weekNum = cnt % d_server.TwentyeightDays     #第几周
        self.dayOfWeek = datetime.today().weekday()

    def SetSignMonth(self,cnt):
        print("SSSSSSSSSSSSSSSSS2222222SetSignMonth222222")
        print(cnt)
        print(self.sizeSignNum)
        if self.signMonth[cnt] == 0 and self.sizeSignNum >= (cnt+1)*7 :
            self.signMonth[cnt] = 1
            self.client.SignMonthPrize(cnt)
        else:
            return

    def setSignNum(self,type,signDay):  #type 签到类型 1是正常签到，2是积分签到 signDay 签到哪天
        recode = 0
        print("SSSSSSSSSSSSSSSSS111111111111111111")
        print(type)
        print(signDay)
        print( self.signNum )
        print(self.dayOfWeek)
        if self.signNum < self.dayOfWeek + 1 :
            if self.signWeek[signDay] == 0 and (type == 1 and signDay == self.dayOfWeek) or (type == 2 and signDay < self.dayOfWeek ):
                self.signWeek[signDay] = type
                cnt = 0
                for i in range(0,len(self.signWeek)):
                    if self.signWeek[i] == 2 :
                        cnt += 1;
                if type == 2 :
                    consume = 2
                    if cnt >0 :
                        for i in range (1,cnt):
                            consume *= 2
                    if  self.score >= consume*d_server.SignScore :
                        self.addScore(-1 * consume*d_server.SignScore, d_server.ScoreType.Sign)
                    else:
                        return
            else:
                return
            self.sizeSignNum += 1
            self.signNum = self.signNum + 1
        else:
            return
        ##加到背包或添加到别的地方
        print("SSSSSSSSSSSSSSSSS44444444444444444444")
        print(d_sign.sign[self.weekNum*7 +signDay]["id"])
        if d_sign.sign[self.weekNum*7 +signDay]["id"] == d_server.TiLiID:
            self.addPhysicalValue(d_sign.sign[self.weekNum*7 +signDay]["num"] )
        elif d_sign.sign[self.weekNum*7 +signDay]["id"] == d_server.JiFenID:
            self.addScore(d_sign.sign[self.weekNum*7 +signDay]["num"], d_server.ScoreSign)
        else:
            self.addItem(d_sign.sign[self.weekNum*7 +signDay]["id"],d_sign.sign[self.weekNum*7 +signDay]["num"])

        self.client.signObtainRsq(signDay,type)

    def getSignTable(self):
        #获取签到表
        print("SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS")
        print(self.signWeek)
        print(self.weekNum)
        self.client.signTableRsq(self.weekNum,self.dayOfWeek,self.sizeSignNum,self.signWeek,self.signMonth)

    def signUp(self):
        curTime = int(time.time())
        if not ComFun.isSameDay(curTime, self.cntTime) :
            #如果跨天
            self.cntTime = curTime
            if datetime.today().weekday() == 0 :  #如果是周一
                self.signWeek = [0, 0, 0, 0, 0, 0, 0]
                self.weekNum += 1
                if int ((curTime - d_server.SetInitTime)/ d_server.TwentyeightDays) > self.whatMonth:
                    self.whatMonth += 1
                    self.weekNum = 0
                    self.sizeSignNum = 0
